import request from '@/utils/request'

/**
 * 根据订单项id查询商铺信息
 */
export function getLogisticsByItemId(orderItemId) {
  return request({
    url: `/order/logistics/${orderItemId}`,
    method: 'get'
  })
}

// 模拟物流数据 - 实际项目中应该由后端提供
export function getMockLogisticsData(orderId, itemId = null) {
  // 模拟多个商品的物流数据
  const mockData = {
    orderInfo: {
      orderId: orderId,
      orderNo: 'NO' + orderId.toString().padStart(10, '0'),
      status: 2, // 1: 待发货, 2: 运输中, 3: 已送达
      createTime: '2024-01-15 10:30:45'
    },
    items: [
      {
        id: '1001',
        name: '苹果手机 iPhone 15',
        image: 'https://example.com/iphone.jpg',
        price: 6999,
        quantity: 1,
        logistics: {
          company: '顺丰速运',
          trackingNo: 'SF1234567890',
          fromLocation: {
            name: '深圳仓库',
            address: '广东省深圳市南山区科技园',
            coordinates: [114.0579, 22.5431]
          },
          status: 2, // 1: 待发货, 2: 运输中, 3: 已送达
          estimatedDelivery: '2024-01-22',
          currentLocation: {
            name: '武汉转运中心',
            address: '湖北省武汉市洪山区光谷大道',
            coordinates: [114.3896, 30.5016],
            updatedAt: '2024-01-19 08:15:30'
          },
          trackingInfo: [
            {
              time: '2024-01-20 08:15:30',
              location: '湖北省武汉市洪山区光谷大道',
              status: '运输中',
              description: '快件在【武汉转运中心】完成分拣，准备发往【北京海淀区公司】',
              coordinates: [114.3896, 30.5016]
            },
            {
              time: '2024-01-19 18:30:00',
              location: '湖北省武汉市',
              status: '运输中',
              description: '快件到达【武汉转运中心】',
              coordinates: [114.3896, 30.5016]
            },
            {
              time: '2024-01-18 20:45:12',
              location: '广东省深圳市',
              status: '已发货',
              description: '快件离开【深圳仓库】，发往【武汉转运中心】',
              coordinates: [114.0579, 22.5431]
            },
            {
              time: '2024-01-18 18:30:55',
              location: '广东省深圳市南山区',
              status: '已揽收',
              description: '顺丰速运已收取快件',
              coordinates: [114.0579, 22.5431]
            },
            {
              time: '2024-01-18 15:20:30',
              location: '广东省深圳市南山区',
              status: '已下单',
              description: '商品已打包，等待快递揽收',
              coordinates: [114.0579, 22.5431]
            }
          ]
        }
      },
      {
        id: '1002',
        name: 'MacBook Pro 16英寸',
        image: 'https://example.com/macbook.jpg',
        price: 18999,
        quantity: 1,
        logistics: {
          company: '京东物流',
          trackingNo: 'JD9876543210',
          fromLocation: {
            name: '上海仓库',
            address: '上海市浦东新区张江高科技园区',
            coordinates: [121.5947, 31.2141]
          },
          status: 2,
          estimatedDelivery: '2024-01-23',
          currentLocation: {
            name: '郑州转运中心',
            address: '河南省郑州市金水区',
            coordinates: [113.6401, 34.7469],
            updatedAt: '2024-01-19 12:30:00'
          },
          trackingInfo: [
            {
              time: '2024-01-19 12:30:00',
              location: '河南省郑州市',
              status: '运输中',
              description: '快件在【郑州转运中心】完成分拣，准备发往【北京朝阳区公司】',
              coordinates: [113.6401, 34.7469]
            },
            {
              time: '2024-01-18 23:15:00',
              location: '河南省郑州市',
              status: '运输中',
              description: '快件到达【郑州转运中心】',
              coordinates: [113.6401, 34.7469]
            },
            {
              time: '2024-01-18 10:20:00',
              location: '上海市',
              status: '已发货',
              description: '快件离开【上海仓库】，发往【郑州转运中心】',
              coordinates: [121.5947, 31.2141]
            },
            {
              time: '2024-01-18 08:45:00',
              location: '上海市浦东新区',
              status: '已揽收',
              description: '京东物流已收取快件',
              coordinates: [121.5947, 31.2141]
            },
            {
              time: '2024-01-18 07:30:00',
              location: '上海市浦东新区',
              status: '已下单',
              description: '商品已打包，等待快递揽收',
              coordinates: [121.5947, 31.2141]
            }
          ]
        }
      }
    ]
  }
  
  // 如果指定了商品ID，则只返回该商品的物流信息
  if (itemId) {
    const item = mockData.items.find(item => item.id === itemId)
    if (item) {
      return Promise.resolve({
        code: 200,
        data: {
          orderInfo: mockData.orderInfo,
          item: item
        },
        message: 'success'
      })
    }
    return Promise.reject(new Error('商品不存在'))
  }
  
  // 否则返回所有商品的物流信息
  return Promise.resolve({
    code: 200,
    data: mockData,
    message: 'success'
  })
} 